﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Library;
using CoreShop.Library.Units;
using CoreShop.Model;
using CoreShop.Model.FromBody;
using CoreShop.Model.UI;
using CoreShop.Repository;
using CoreShop.Services.Services.System;
using Model.ProductModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using SqlSugar.Extensions;
using Yitter.IdGenerator;
using static System.Net.WebRequestMethods;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class PosRemoteServices : Repository<FromBodyModel>, IPosRemoteServices
    {
        public string baseUrl = "https://api.yzipos.com";//"https://betaapi.yzipos.com";
        public string _token;


        /// <summary>
        /// 服务名称
        /// </summary>
        public struct SERVER
        {
            /// <summary>
            /// 商品服务
            /// </summary>
            public static readonly string Product = "/product";

            /// <summary>
            /// 基础服务
            /// </summary>
            public static readonly string Base = "/base";

            /// <summary>
            /// 系统服务
            /// </summary>
            public static readonly string System = "/system";

            /// <summary>
            /// 收银机服务
            /// </summary>
            public static readonly string Pos_service = "/pos-service";

            /// <summary>
            /// 会员服务
            /// </summary>
            public static readonly string Member = "/member";

            /// <summary>
            /// 订单服务
            /// </summary>
            public static readonly string Order = "/order";

            /// <summary>
            /// 券服务
            /// </summary>
            public static readonly string Coupon = "/coupon";

            /// <summary>
            /// 支付服务
            /// </summary>
            public static readonly string Pay = "/pay";

            /// <summary>
            /// 活动
            /// </summary>
            public static readonly string MarketActivity = "/marketactivity";
        }

        /// <summary>
        /// 接口名称
        /// </summary>
        public struct API
        {
            /// <summary>
            /// 用户注册
            /// </summary>
            public static string userRegister = "/base/device/feign/baseDevice/register";

            /// <summary>
            /// 用户登录
            /// </summary>
            public static string loginByPos = "/auth/loginByPos";

            /// <summary>
            /// 获取券详情
            /// </summary>
            public static string GetCoupon = "/couponInfo/getCouponInfoByNo";
            /// <summary>
            /// 核销券
            /// </summary>
            public static string ConsumeCoupon = "/couponInfo/consumeCoupon";

            /// <summary>
            /// 获取商品凋价单
            /// </summary>
            public static string GetChgpriceList = "/product/feign/getChgpriceList";

            /// <summary>
            /// 获取商品信息
            /// </summary>
            public static string GetPdtSpuBranch = "/product/feign/getPdtSpuBranch";

            /// <summary>
            /// 获取支付方式
            /// </summary>
            public static string GetBalanceType = "/feign/baseTenantBalanceType/list";
            
            /// <summary>
            /// 发送订单
            /// </summary>
            public static string InsertPosOrder = "/feign/insertPosOrder";

            /// <summary>
            /// 发起支付
            /// </summary>
            public static string CreatePayOrder = "/pay/feign/createOrder";

            /// <summary>
            /// 退款接口
            /// </summary>
            public static string RefundPayOrder = "/pay/feign/refundOrder";
            /// <summary>
            /// 查询在线订单详情
            /// </summary>
            public static string SearchPosOrder = "/feign/searchPosOrder";
            /// <summary>
            /// 会员注册
            /// </summary>            
            public static string VipRegister = "/vip/feign/vipRegister";
            /// <summary>
            /// 会员注册
            /// </summary>            
            public static string GetVipList = "/vip/info/manager/getList";

            /// <summary>
            /// 会员修改
            /// </summary>            
            public static string VipUpdateInfo = "/vip/info/manager/updateInfoAndCard";

            /// <summary>
            /// 获取会员信息
            /// </summary>
            public static string GetVipInfo = "/vip/feign/getVipInfoByVipTel20";//public static string GetVipInfo = "/vip/feign/getVipInfoByVipTel";

            /// <summary>
            /// 获取会员信息集合 可模糊查询
            /// </summary>
            public static string GetViplistBy = "/vip/feign/listVipInfoByVipTel";

            /// <summary>
            /// 会员权益
            /// </summary>
            public static string GetVipLeverRight = "/vip/feign/getVipOfflineLevelRight";

            /// <summary>
            /// 修改密码
            /// </summary>
            public static string RestPassword = "/vip/feign/restPassword";

            /// <summary>
            /// 更新会员卡状态
            /// </summary>
            public static string UpdateCardStatus = "/vip/feign/updateCardStatus";

            /// <summary>
            /// 获取会员等级列表
            /// </summary>
            public static string GetLevelList = "/vip/feign/getLevelList";

            /// <summary>
            /// 获取发卡的类型
            /// </summary>
            public static string GetCardTypeList = "/vip/feign/getCardTypeList";

            /// <summary>
            /// 获取储值卡信息
            /// </summary>
            public static string GetCardByCardNo = "/vip/feign/getCardByCardNo";

            /// <summary>
            /// 会员发卡
            /// </summary>
            public static string SendGenerateCard = "/vip/feign/frontGenerateCard";
            /// <summary>
            /// 会员发卡并注册
            /// </summary>
            public static string VipRegistration = "/vip/feign/oneClickRegistration";

            /// <summary>
            /// 获取会员的券列表
            /// </summary>
            public static string GetCouponVip = "/coupon/feign/getCouponVipRelation";

            /// <summary>
            /// 获取库存
            /// </summary>
            public static string GetInvCurrentInv = "/inv/feign/invCurrentInv/list";

            /// <summary>
            /// 今日调价
            /// </summary>
            public static string ChgpriceHeadByPos = "/pdtSpuBranch/feign/insertPdtChgpriceHeadByPos";

            /// <summary>
            /// 获取树状菜单结构
            /// </summary>
            public static string GetItemSortTreeList = "/pdtItemSort/feign/pdtItemSortTreeList";

            /// <summary>
            /// 获取树状菜单结构
            /// </summary>
            public static string GetItemSortTreeList20 = "/pdtItemSort/feign/pdtItemSortTreeList20";

            /// <summary>
            /// 获取商品单位信息
            /// </summary>
            public static string GetPdtUomByPosList = "/product/feign/getPdtUomByPosList";

            /// <summary>
            /// 获取员工资料
            /// </summary>
            public static string Base_EmployeeList = "/employeeInfo/feign/getlistEmployeeInfo";

            /// <summary>
            /// 下载副屏图片
            /// </summary>
            public static string PosTenantScreen = "/feign/posTenantScreen/list";

            /// <summary>
            /// 获取生成商品条码
            /// </summary>
            public static string Product_Generate = "/pdtSpuInfo/itemCode/generate";

            /// <summary>
            /// 查询线下营销活动列表
            /// </summary>
            public static string ActivityList = "/activity/feign/activityList20";

            /// <summary>
            /// 查询活动详情
            /// </summary>
            public static string ActivityDetail = "/activity/feign/activityDetail";

            /// <summary>
            /// 获取分类编码
            /// </summary>
            public static string GetItemSortCode = "/item/sort/getItemSortCode";

            /// <summary>
            /// 新增分类
            /// </summary>
            public static string InsertPdtItemSort = "/pdtItemSort/feign/insertPdtItemSort";
            /// <summary>
            /// 发送验证码
            /// </summary>
            public static string SendSMS = "/sms/feign/sendSMS";

            /// <summary>
            /// 获取商品单位
            /// </summary>
            public static string GetPdtUomList = "/feign/product/feign/getPdtUomList";

            /// <summary>
            /// 新增商品
            /// </summary>
            public static string AddSaveForPos = "/feign/product/feign/addSaveForPos20";

            /// <summary>
            /// 获取Ai激活码
            /// </summary>
            public static string GetBaseAiList = "/baseAi/getBaseAiList";

            /// <summary>
            /// 新增已激活Ai码到序列中
            /// </summary>
            public static string InsertBaseAiBind = "/baseAiBind/insertBaseAiBind";

            /// <summary>
            /// 会员积分冲
            /// </summary>
            public static string SaveVipPointsForPos = "/vip/points/saveVipPointsForPos";

            /// <summary>
            /// 交班记录
            /// </summary>
            public static string InsertPosDuty = "/posDuty/insertPosDuty";///feign/insertPosDuty

            /// <summary>
            /// 查询商户支付列表
            /// </summary>
            public static string ThirdpartyAccoun = "/payThirdpartyAccount/searchPayThirdpartyAccountList";

            /// <summary>
            /// 查询商户资料
            /// </summary>
            public static string SystemTenant = "/tenant";

            /// <summary>
            /// 查询服务商资料
            /// </summary>
            public static string SearchSysAgent = "/sysAgent/searchSysAgent";

            /// <summary>
            /// 会员集会档位
            /// </summary>
            public static string PosStandardList = "/vip/feign/posStandardList";

            /// <summary>
            /// 查询前台参数值
            /// </summary>
            public static string PosPara = "/basePosParamValue/searchBasePosParamValueList";

            /// <summary>
            /// 获取功能权限
            /// </summary>
            public static string GetRouters = "/sysposmenu/getRouters";

            /// <summary>
            /// 获取功能权限
            /// </summary>
            public static string GetUserRole = "/sysposrole/details";

            /// <summary>
            /// 查询小程序收银订单列表
            /// </summary>
            public static string GetMallOrderList = "/api/xcxorderhead/order-list-pos";

            /// <summary>
            /// 商品价格修改
            /// </summary>
            public static string SpuSaPriceUpdate = "/product/feign/pdtSpuSpecUpdateSaPrice";

            /// <summary>
            /// 门店零售价修改
            /// </summary>
            public static string BranchPriceUpdate = "/product/feign/pdtSpuBranchUpdateSaPrice";

            /// <summary>
            /// 资料修改记录
            /// </summary>
            public static string GetSysRecordEntity = "/sysRecordEntity/feign/getSysRecordEntity";

            /// <summary>
            /// 今日调价
            /// </summary>            
            public static string ChgPriceHeadByPos = "/pdtSpuBranch/feign/insertPdtChgpriceHeadByPos20";

            /// <summary>
            /// 改变订单状态
            /// </summary>
            public static string UpdateOrderStatus = "/api/xcxOrderHead/update-order";

            /// <summary>
            /// 新增支付
            /// </summary>
            public static string BatchInsertPayInfo = "/payThirdpartyAccount/batchInsertPayThirdpartyAccount";

            /// <summary>
            /// 支付列表
            /// </summary>
            public static string GetPayAccuntList = "/payThirdpartyAccount/searchPayThirdpartyAccountList";

            /// <summary>
            /// 获取可以积分兑换商品的列表
            /// </summary>
            public static string GetIntegralGoodsList = "/itemIntegral/feign/getList";

            /// <summary>
            /// 获取可以积分兑换商品的列表
            /// </summary>
            public static string SetIntegralExchange = "/itemIntegral/feign/itemIntegralExchange";

            /// <summary>
            /// 获取商品库商品
            /// </summary>
            public static string GetPdtStanderSpuInfo = "/product/feign/product/feign/getPdtStanderSpuInfoList";

            /// <summary>
            /// 保存支付方式
            /// </summary>
            public static string SaveBalanceType = "/baseTenantBalanceType/editSave";
        }

        /// <summary>
        /// 支付操作
        /// </summary>
        public struct PAYTYPE
        {
            /// <summary>
            /// 发起付款
            /// </summary>
            public static readonly string PAY = "AUTHCODE";
            /// <summary>
            /// 退款
            /// </summary>
            public static readonly string BALANCE = "BALANCE";
        }


        public PosRemoteServices(string token = "")
        {
            _token = token;
        }
               
           
        /// <summary>
        /// 查询线下营销活动列表
        /// </summary>
        /// <returns></returns>
        public ReturnModel GetActivityList(sysuser sysuser, string UpdatedateTime = null)
        {
            JObject keyValues = new JObject();
            if (!string.IsNullOrEmpty(UpdatedateTime))
            {
                keyValues.Add(new JProperty("startTime", UpdatedateTime));
            }

            string postStr = !string.IsNullOrEmpty(JsonConvert.SerializeObject(keyValues)) ? JsonConvert.SerializeObject(keyValues) : "{}";

            return HttpHelper.PostSendMessage(baseUrl, $"{SERVER.MarketActivity}{API.ActivityList}", postStr, 60, "", sysuser);
        }


        /// <summary>
        /// 下载线下营销活动列表20
        /// </summary>
        public int GetActivityList(corecmsstore store, sysuser sysuser, corecmsexchange exchange, ref string msg)
        {
            try
            {
                var result = GetActivityList(sysuser, exchange.activityTime.ToString());
                if (result == null || string.IsNullOrEmpty(result.MessageOrString))
                {
                    return 0;
                }
                ApiReturnModel apiReturnModel = result.MessageOrString.JsonToEntity<ApiReturnModel>();
                if ((apiReturnModel == null) || (apiReturnModel.data == null)) return 0;
                List<activitymaster> activitymasters = apiReturnModel.data.ToString().JsonToEntity<List<activitymaster>>();
                if (activitymasters != null)
                {
                    foreach (var activitymaster in activitymasters)
                    {
                        activitymaster.weekdayType = CommFunc.ConvertToInt32(activitymaster.weekdayType);
                        activitymaster.activityBeginTime = activitymaster.activityBeginDate.TimeOfDay;
                        activitymaster.activityEndTime = activitymaster.activityEndDate.TimeOfDay;
                    }
                    new activitymasterServices().InsertOrUpdateAsync(activitymasters);
                }                    

                List<activitysort> activitysorts = new List<activitysort>();
                List<activitygoods> activitygoods = new List<activitygoods>();
                List<activitylineresp> activitylineresps = new List<activitylineresp>();
                List<activitygoodsexclude> goodsexcludes = new List<activitygoodsexclude>();

                DateTime dtTime;
                foreach (activitymaster master in activitymasters)
                {
                    if ((master.updateTime != null) && (master.updateTime.ToString() != ""))
                    {
                        dtTime = CommFunc.ConvertToDateTime(master.updateTime);
                        if (dtTime > exchange.activityTime) exchange.activityTime = dtTime;
                    }
                    if ((master.createTime != null) && (master.createTime.ToString() != ""))
                    {
                        dtTime = CommFunc.ConvertToDateTime(master.createTime);
                        if (dtTime > exchange.activityTime) exchange.activityTime = dtTime;
                    }

                    if (master.activityInfoId > exchange.activityId)
                        exchange.activityId = master.activityInfoId;

                    if (master.sortList != null)
                    {
                        foreach (var item in master.sortList)
                        {
                            item.excludeFLag = false;//包括的分类
                            activitysorts.Add(item);
                        }
                    }

                    if (master.excludeGoodsSortList != null)
                    {
                        foreach (var item in master.excludeGoodsSortList)
                        {
                            item.activityGoodsSortId = item.activityExcludeGoodsSortId;
                            item.excludeFLag = true;//排除的分类
                            activitysorts.Add(item);
                        }
                    }

                    if (master.goodsList != null)
                        activitygoods.AddRange(master.goodsList);

                    if (master.lineRespList != null)
                    {
                        foreach (var item in master.lineRespList)
                        {
                            item.activitySubTypeId = master.activitySubTypeId;
                        }
                        activitylineresps.AddRange(master.lineRespList);
                    }                       

                    if (master.excludeGoodsList != null)
                        goodsexcludes.AddRange(master.excludeGoodsList);
                }
                if ((activitygoods != null) && (activitygoods.Count > 0))
                {
                    new activitygoodsServices().InsertOrUpdateAsync(activitygoods);

                    //更新数据，因为后台现在返回的，没有规格ID
                    string msql = @"update activitygoods s, corecmsfood c set s.pdtSpuSpecId = c.foodSpecId "
                        + "where s.spuId = c.id and s.pdtSpuSpecId is null";
                    base.ExecuteCommand(msql);
                }

                if ((activitylineresps != null) && (activitylineresps.Count > 0))
                    new activitylinerespServices().InsertOrUpdateAsync(activitylineresps);

                if ((goodsexcludes != null) && (goodsexcludes.Count > 0))
                {
                    new activitygoodsexcludeServices().InsertOrUpdateAsync(goodsexcludes);

                    //更新数据，因为后台现在返回的，没有规格ID
                    string msql = @"update activitygoodsexclude s, corecmsfood c set s.pdtSpuSpecId = c.foodSpecId "
                        + "where s.spuId = c.id and s.pdtSpuSpecId is null";
                    base.ExecuteCommand(msql);
                }

                if ((activitysorts != null) && (activitysorts.Count > 0))
                    new activitysortServices().InsertOrUpdateAsync(activitysorts);
                
                new corecmsexchangeServices().UpdateAsync(new corecmsexchange { storeId = store.id, activityId = exchange.activityId, activityTime = exchange.activityTime });

                return activitymasters.Count();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return -1;
            }
        }
                
        #region 商品分类

        public List<GsSordItemClassModel> GetSortChildren(List<GsSordItemClassModel> gsSordItemClasses, List<GsSordItemClassModel> gsSordListInfo)
        {
            gsSordItemClasses.ForEach(gs =>
            {
                if (gs.Children == null)
                {
                    gsSordListInfo.Add(gs);
                }
                else
                {
                    gsSordListInfo.Add(gs);
                    GetSortChildren(gs.Children, gsSordListInfo);
                }
            });

            return gsSordListInfo;
        }

        /// <summary>
        /// 商品
        /// </summary>
        /// <param name="sWebUrl"></param>
        /// <param name="ht"></param>
        /// <param name="jToken"></param>
        /// <returns></returns>
        public int GetGoodCategoryList(corecmsstore store, sysuser user, corecmsexchange exchange, ref string msg)
        {
            try
            {
                var url = string.Concat(baseUrl, $"{SERVER.Product}{API.GetItemSortTreeList20}");
                var sysDataPost = new SysDataPost();
                sysDataPost.startTime = CommFunc.ConvertDateTimeToString(exchange.foodCategoryTime);

                string postStr = JsonConvert.SerializeObject(sysDataPost);
                string retStr = Library.HttpHelper.httpPost(url, postStr, user.ccessToken);
                AdminUiCallBack res = JsonConvert.DeserializeObject<AdminUiCallBack>(retStr);
                if (res.code.Equals(401))
                    throw new Exception(res.code.ToString());

                List<GsSordItemClassModel> dataList = JsonConvert.DeserializeObject<List<GsSordItemClassModel>>(Convert.ToString(res.data));
                if (dataList.Count > 0)
                {
                    List<corecmsfoodcategory> foodCategoryList = new List<corecmsfoodcategory>();
                    foodCategoryList.AddRange(ModelHelper.GoodsCategoryToFoodCateory(dataList, store));

                    var returnValue = new corecmsfoodcategoryServices().InsertOrUpdate(foodCategoryList);

                    DateTime createTime = dataList.Select(p => p.createTime.ObjToDate()).Max();
                    DateTime updateTime = dataList.Select(p => p.updateTime.ObjToDate()).Max();

                    if (updateTime > createTime) createTime = updateTime;

                    exchange.foodCategoryId = foodCategoryList.Select(p => p.id).Max();
                    exchange.foodCategoryTime = CommFunc.ConvertToDateTime(createTime).AddSeconds(1);
                    new corecmsexchangeServices().UpdateAsync(new corecmsexchange { storeId = store.id, foodCategoryId = exchange.foodCategoryId, foodCategoryTime = exchange.foodCategoryTime });
                }
                return dataList.Count;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                //if (ex.Message.Equals("401"))
                //{
                //    msg = "用户登录已过期";
                //    RefreshToken(store, user, exchange);
                //}
                exchange.status = false;
                return -1;
            }
        }

        /// <summary>
        /// 商品单位
        /// </summary>
        /// <param name="sWebUrl"></param>
        /// <param name="ht"></param>
        /// <param name="jToken"></param>
        /// <returns></returns>
        public int GetGoodUnitList(corecmsstore store, sysuser user, corecmsexchange exchange, ref string msg)
        {
            try
            {
                var url = string.Concat(baseUrl, $"{SERVER.Product}{API.GetPdtUomByPosList}");

                //url = string.Concat("https://betaapi.yzipos.com", $"{SERVER.Product}{API.GetPdtUomByPosList}");

                var sysDataPost = new SysDataPost();
                sysDataPost.startTime = CommFunc.ConvertDateTimeToString(exchange.foodUnitTime);
                //sysDataPost.startTime = "2021-02-02 00:02:01";

                string postStr = JsonConvert.SerializeObject(sysDataPost);
                string retStr = Library.HttpHelper.httpPost(url, postStr, user.ccessToken);
                AdminUiCallBack res = JsonConvert.DeserializeObject<AdminUiCallBack>(retStr);
                if (res.code.Equals(401))
                    throw new Exception(res.code.ToString());

                List<corecmsfoodunit> dataList = JsonConvert.DeserializeObject<List<corecmsfoodunit>>(Convert.ToString(res.data));
                if (dataList.Count > 0)
                {
                    var returnValue = new corecmsfoodunitServices().InsertOrUpdate(dataList);

                    DateTime createTime = dataList.Select(p => p.createTime.ObjToDate()).Max();
                    DateTime updateTime = dataList.Select(p => p.updateTime.ObjToDate()).Max();

                    if (updateTime > createTime) createTime = updateTime;

                    exchange.foodUnitId = dataList.Select(p => p.uomId).Max();
                    exchange.foodUnitTime = CommFunc.ConvertToDateTime(createTime).AddSeconds(1);
                    new corecmsexchangeServices().UpdateAsync(new corecmsexchange { storeId = store.id, foodUnitId = exchange.foodUnitId, foodUnitTime = exchange.foodUnitTime });
                }
                return dataList.Count;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                //if (ex.Message.Equals("401"))
                //{
                //    msg = "用户登录已过期";
                //    RefreshToken(store, user, exchange);
                //}
                exchange.status = false;
                return -1;
            }
        }

        #endregion

        /// <summary>
        /// 初始化副屏图片
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public ReturnModel GetScreenImage(sysuser sysuser, corecmsstore sysStore)
        {
            List<object> strings = new List<object>();
            JObject keyValues = new JObject();

            strings.Add(sysStore.branchId);
            keyValues.Add(new JProperty("branchIds", strings));

            ReturnModel result = HttpHelper.PostSendMessage(baseUrl,
                    $"{SERVER.Order}{API.PosTenantScreen}",
                    keyValues.ToJsonString(), 60, "", sysuser);

            return result;
        }
    
        /// <summary>
        /// 商品
        /// </summary>
        /// <param name="sWebUrl"></param>
        /// <param name="ht"></param>
        /// <param name="jToken"></param>
        /// <returns></returns>
        public int GetEmployeeList(corecmsstore store, sysuser user, corecmsexchange exchange, ref string msg)
        {
            try
            {
                var sysDataPost = new SysDataPost();
                sysDataPost.startTime = CommFunc.ConvertDateTimeToString(exchange.employeeTime);

                string postStr = JsonConvert.SerializeObject(sysDataPost);
                var url = string.Concat(baseUrl, $"{SERVER.Base}{API.Base_EmployeeList}");//新版本
                string result = Library.HttpHelper.httpPost(url, postStr, user.ccessToken);

                ApiReturnModel res = JsonConvert.DeserializeObject<ApiReturnModel>(result);

                if (res.code.Equals(401))
                    throw new Exception(res.code.ToString());

                var resultModel = result.JsonToEntity<GetGsEmployeeModel>();                
                if (resultModel.data != null)
                {
                    List<GsEmployeeModel> gsEmployees = resultModel.data.ToString().JsonToEntity<List<GsEmployeeModel>>();
                    List<corecmsemployee> employeeList = new List<corecmsemployee>();
                    foreach (var item in gsEmployees)
                    {
                        employeeList.Add(ModelHelper.ConvrtGsToEmployee(item, store));
                        if (exchange.employeeId < item.employeeId)
                            exchange.employeeId = item.employeeId;

                        if (item.createTime > exchange.employeeTime)
                            exchange.employeeTime = item.createTime;

                        if (item.updateTime > exchange.employeeTime)
                            exchange.employeeTime = CommFunc.ConvertToDateTime(item.updateTime);
                    }
                    var returnValue = new corecmsemployeeServices().InsertOrUpdate(employeeList);
                    if (returnValue)
                    {
                        new corecmsexchangeServices().UpdateAsync(new corecmsexchange { storeId = store.id,employeeId = exchange.employeeId, employeeTime = exchange.employeeTime });
                        return employeeList.Count;
                    }    
                }
                return 0;
            }
            catch (Exception ex)
            {
                msg = ex.Message;                
                exchange.status = false;
                return -1;
            }
        }

        /// <summary>
        /// 查询前端参数
        /// </summary>
        public int GetPosPara(corecmsstore store, sysuser sysuser, corecmsexchange exchange, ref string msg)
        {            
            try
            {
                var result = HttpHelper.PostSendMessage(baseUrl, $"{SERVER.Base}{API.PosPara}", "{}", 60, "", sysuser);
                BaseSettingListInfoModel baseSettingInfo = result.MessageOrString.JsonToEntity<BaseSettingListInfoModel>();

                if (baseSettingInfo == null || baseSettingInfo.data == null)
                {
                    return 0;
                }

                if (baseSettingInfo.code != 200) return 0;
                var data = baseSettingInfo.data;
                syssetting syssetting = new syssetting();
                syssetting.corpId = sysuser.corpId;
                syssetting.verifyPayPassword = data.verifyPayPassword;
                syssetting.bonusSetting = data.bonusSetting;
                syssetting.bonusRatio = data.bonusRatio;
                new syssettingServices().InsertOrUpdateAsync(syssetting);
                return 1;
            }
            catch (Exception ex)
            {
                msg = ex.Message;                
                exchange.status = false;
            }
            return 0;
        }

        /// <summary>
        /// 获取在线订单数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnModel GetOrderListXcx(corecmsstore store, sysuser sysuser, corecmsexchange exchange)
        {
            JObject keyValues = new JObject();
            keyValues.Add(new JProperty("branchId", store.branchId));
            keyValues.Add(new JProperty("tenantId", store.corpId));
            keyValues.Add(new JProperty("userPhone", ""));
            keyValues.Add(new JProperty("status", "3"));
            keyValues.Add(new JProperty("posFlag", "1"));
            keyValues.Add(new JProperty("startTime", CommFunc.ConvertDateTimeToString(exchange.mallOrderTime)));
            return HttpHelper.PostSendMessage(baseUrl, $"{SERVER.MarketActivity}{API.GetMallOrderList}", keyValues.ToString(), 60, "", sysuser);
        }

        /// <summary>
        /// 查询线下营销活动列表
        /// </summary>
        /// <returns></returns>
        public List<sysrecordentity> GetSysRecordEntity(sysuser sysuser,ref string msg)
        {
            try
            {
                var url = string.Concat(baseUrl, $"{SERVER.System}{API.GetSysRecordEntity}", "?tenantId=", sysuser.corpId);
                var returnModel = HttpHelper.PostSendMessage(url, "", "", 60, "", sysuser);
                BaseReturnModel baseReturn = returnModel.MessageOrString.JsonToEntity<BaseReturnModel>();
                if (baseReturn.code != 200 || baseReturn.data == null)
                {
                    msg = $"获取数据出错:{baseReturn.msg}";
                    return null;
                }
                List<sysrecordentity> list = baseReturn.data.ToString().JsonToEntity<List<sysrecordentity>>();
                return list;
            }
            catch (Exception ex) {
                msg = ex.Message;
            }
            return null;            
        }

    }

}